/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月23日
 */
package com.massyframework.beanskin.graphql.context;

import java.util.Objects;

import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.assembly.AssemblyContext;
import com.massyframework.beanskin.assembly.AssemblyNotReadiedException;
import com.massyframework.beanskin.service.ServiceNotFoundException;
import com.massyframework.beanskin.service.ServiceRepository;

import graphql.schema.DataFetcher;

/**
 * {@link AssemblyDataFetcher}, 装配件数据获取器
 * @author huangkaihui
 *
 */
public abstract class AssemblyDataFetcher<T> 
	implements DataFetcher<T>{
	
	private final Assembly assembly;
	private final ServiceRepository serviceRepo;

	/**
	 * 
	 */
	public AssemblyDataFetcher(Assembly assembly) {
		this.assembly =
			Objects.requireNonNull(assembly, "\"assembly\" cannot be null.");
		this.serviceRepo =
			ServiceRepository.retrieveFrom(this.assembly);
	}
	
	
	/**
	 * 装配件是否准备就绪
	 * @param assembly {@link Assembly}
	 * @return {@link boolean},返回<code>true</code>表示准备就绪，否则返回<code>False</code>
	 */
	protected boolean readied() {
		return this.assembly.isWorking();
	}
	
	/**
	 * 获取服务
	 * @param serviceType {@link Class},服务类型
	 * @return {@link S}
	 * @throws AssemblyNotReadiedException
	 */
	protected <S> S getService(Class<S> serviceType) throws ServiceNotFoundException {
		return this.getService(serviceType, null);
	}
	
	/**
	 * 获取服务
	 * @param serviceType {@link Class},服务类型
	 * @param filterString {@link String},过滤字符串
	 * @return {@link S}
	 * @throws AssemblyNotReadiedException
	 */
	protected <S> S getService(Class<S> serviceType, String filterString) throws ServiceNotFoundException {
		return this.serviceRepo.getService(serviceType, filterString);
	}
	
	/**
	 * 获取装配件上下文
	 * @return {@link AssemblyContext}
	 * @throws AssemblyNotReadiedException
	 */
	public AssemblyContext getAssemblyContext() throws AssemblyNotReadiedException {
		if (assembly.isWorking()) {
			return assembly.getAssemblyContext();
		}
		
		throw new AssemblyNotReadiedException(assembly);
	}
	
	/**
	 * 根据{@code beanName}获取Bean，并返回指定类型
	 * @param <T>
	 * @param name {@link String}, bean名称
	 * @param beanType {@link Class}, bean类型
	 * @return {@link T}
	 */
	public <B> B getBean(String beanName, Class<B> beanType) {
		return this.getAssemblyContext().getBean(beanName, beanType);
	}
	
	/**
	 * 所属装配件
	 * @return {@link Assembly}
	 */
	public Assembly getAssembly() {
		return this.assembly;
	}

}
